package eft.android.controller;


import java.util.ArrayList;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import me.croute.calendarexample.CrossFitApp;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;
import eft.android.model.CrossfitDataManage;
import eft.android.view.AddWorkout;
import eft.android.view.DayView;

public class Controller extends Activity{
	
	CrossfitDataManage dbManager;
	
	String savedTitle;
	String savedDescrip;
	String savedReps;
	String savedWeight; 
	String savedTime;
	String savedNotes;
	
	private String[] workoutTitleArray;
	
	public Controller(){
		Context cntxt = CrossFitApp.getAppContext();
        dbManager = new CrossfitDataManage( cntxt );
       // dbManager.open();
	}
	
	public static Controller getInstance(){
		return new Controller();
	}

	public void addWorkoutCancel(View v) {
		// TODO Auto-generated method stub
	}
	
	/* takes in strings from the addWorkout class and converts then to a double for saving in database */
	private long convertTimeToLong( String timeMins, String timeSecs ){
		
		double t = Double.parseDouble( timeMins.trim() );
		t = t * 60 * 100;
		
		t = Math.floor(t);
		
		double t2 = Double.parseDouble( timeSecs.trim() );
		t2 = t2 * 100;
		
		t2 = Math.floor(t2);
		
		long totalTime = (long) ( t + t2 );
		
		return totalTime;
	}
	

	/*public void storeUserWorkout(String woTitle, String descrip,
			String reps, String weight, String time, String notes) {
	}
	*/

	public void addWorkout(String woTitle, String descrip, Long date, String reps,
			String weight, String timeMins, String timeSecs, String notes) {
		
		long totalTime = convertTimeToLong(timeMins, timeSecs);
		
		dbManager.addNewExcercise(woTitle, 0, descrip, date, reps, totalTime, weight, notes);
		
		
		// TODO Auto-generated method stub
		
	}

	public void selectCFWorkout(String date) {
		// TODO Auto-generated method stub
		
	}
	
	private String[]  listToStringArray(List<Workout> l){
		
		Iterator<Workout> itr = l.iterator();
		
		String[] titles = new String[l.size() + 1];
		int i = 0;
		
		while( itr.hasNext() ){
			titles[i] = itr.next().getTitle();
			i++;
		}
		return titles;
	}
	
	public List<Workout>  listUserWorkouts() {
		List<Workout> w = new ArrayList<Workout>(dbManager.getAllSavedWorkouts(false));
		List<Workout> ret = new ArrayList<Workout>();
		Workout wk;
		Iterator<Workout> itr = w.iterator();
		while(itr.hasNext())
		{
			wk = (Workout) itr.next();
			if(wk.getType() == 0)
			{
				ret.add(wk);
			}		
		}
		
		return ret;
	}
	
	public List<Workout> listGirlWorkouts() {
		List<Workout> w = dbManager.getAllSavedWorkouts(true);
		List<Workout> ret = new ArrayList<Workout>();
		Workout wk;
		Iterator<Workout> itr = w.iterator();
		while(itr.hasNext())
		{
			wk = (Workout) itr.next();
			if(wk.getType() == 1)
			{
				ret.add(wk);
			}		
		}
		
		return ret;
	}
	
	public List<Workout> listHeroWorkouts() {
		List<Workout> w = dbManager.getAllSavedWorkouts(true);
		List<Workout> ret = new ArrayList<Workout>();
		Workout wk;
		Iterator<Workout> itr = w.iterator();
		while(itr.hasNext())
		{
			wk = (Workout) itr.next();
			if(wk.getType() == 2)
			{
				ret.add(wk);
			}		
		}
		
		
		return ret;
	}

	
	public List<Workout> listSpecialWorkouts() {
		List<Workout> w = dbManager.getAllSavedWorkouts(true);
		List<Workout> ret = new ArrayList<Workout>();
		Workout wk;
		Iterator<Workout> itr = w.iterator();
		while(itr.hasNext())
		{
			wk = (Workout) itr.next();
			if(wk.getType() == 3)
			{
				ret.add(wk);
			}		
		}
		
		return ret;
	}
	
	public List<Workout> getAllUserWorkouts() {
		ArrayList<Workout> w = new ArrayList<Workout>(dbManager.getAllSavedWorkouts(false));
		List<Workout> ret = new ArrayList<Workout>();
		Workout wk;
		Iterator<Workout> itr = w.iterator();
		while(itr.hasNext())
		{
			wk = (Workout) itr.next();
			if(wk.getType() == 0)
			{
				ret.add(wk);
			}		
		}
		return ret;
	}
	
	public List<Workout> getAllUserWorkoutsByDate(long date) {
		List<Workout> w = getAllUserWorkouts();
		List<Workout> ret = new ArrayList<Workout>();
		Workout wk;
		Iterator<Workout> itr = w.iterator();
		while(itr.hasNext())
		{
			wk = (Workout) itr.next();
			if(wk.getDate() == date)
			{
				ret.add(wk);
			}		
		}
		
		return ret;
	}
	
	public static long createDateLong( int day, int month, int year ){
		
		String dayString = ((Integer) day).toString();
		String monthString = ((Integer) month).toString();
		String yearString = ((Integer) year).toString();
		
		if( dayString.length() == 1 ){
			dayString = ("0" + dayString);
		}
		if( monthString.length() == 1 ){
			monthString = ("0" + monthString);
		}
		String formattedDate =  yearString + monthString + dayString;
		
		return Long.parseLong(formattedDate);
	}

	public void saveWeight(String weight, int day, int month, int year) {
		// TODO Auto-generated method stub
		long date = createDateLong(day, month, year);
		
		dbManager.addDailyWeight(Double.parseDouble(weight), date);
		
	}

	public double getWeightByDay(int day, int month, int year) {
		// TODO Auto-generated method stub
		long date = createDateLong(day, month, year);
		
		Weight w = dbManager.getWeightEntry(date);
		if( w == null ){ return 0; }
		return w.getWeight();
	}
	
	public List<Workout> dateOrder(List<Workout> w)
	{
		  Collections.sort(w, new Comparator<Workout>() {
		        @Override public int compare(Workout w1, Workout w2) {
		            if(w1.getDate() == w2.getDate()) return 0;
		            if(w1.getDate() < w2.getDate()) return -1;
		            else return 1;
		        }
		    });

		return w;
	}
	
	public List<Workout> timeOrder(List<Workout> w)
	{
		  Collections.sort(w, new Comparator<Workout>() {
		        @Override public int compare(Workout w1, Workout w2) {
		            if(w1.getTime() == w2.getTime()) return 0;
		            if(w1.getTime() < w2.getTime()) return -1;
		            else return 1;
		        }
		    });

		return w;
	}
	
		public List<Workout> nameOrder(List<Workout> w)
		{
			if(w.equals(null)) return null;
			Collections.sort(w, new Comparator<Workout>() {
		        @Override public int compare(Workout w1, Workout w2) {
		            return w1.getTitle().compareTo(w2.getTitle());
		        }

		    });
			return w;
		}
		
		public void deleteWorkout(Workout w)
		{
			
			dbManager.deleteWorkout(w);
		}
		
		public void deleteWeight(long date)
		{
			
			dbManager.deleteWeight(date);
		}
		/* for passing from AddWorkout to the database */
		public void updateWorkout(long id, String woTitle, String descrip, Long date, String reps,
				String weight, String timeMins, String timeSecs, String notes)
		{	
			long t = convertTimeToLong( timeMins, timeSecs );
			Workout w = new Workout(id, 0, woTitle, descrip, date, weight, reps, t, notes);
			dbManager.updateWorkout(w);
		}
		
		/* for passing from DayView to the AddWorkout screen */
		public void updateWorkout(Workout w)
		{	
			

		}
/*
		public void addNewWOfromCF(Workout workout) {
			Intent gotoaddWorkout = new Intent( getApplicationContext(), AddWorkout.class );
			
			gotoaddWorkout.putExtra(  "Title", workout.getTitle());
			gotoaddWorkout.putExtra( "Description", workout.getDescription());
			gotoaddWorkout.putExtra(  "Weight", workout.getWeight());	
			gotoaddWorkout.putExtra(  "Reps", workout.getReps());	
			
			
			startActivity(gotoaddWorkout);
		}
	*/	
		

/*
	public Cursor getCursor() {
		// TODO Auto-generated method stub
		Cursor workoutsCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(workoutsCursor);
        
        return workoutsCursor;
	}
	*/

}
